package com.kaifamiao.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionTest1 {

	public static void main(String[] args) {
		
		// JDK 1.7 之前的写法
		// Collection<String> c = new ArrayList<String>() ;
		
		// 从 JDK 1.7 开始支持 钻石运算符 (菱形语法)
		Collection<String> c = new ArrayList<>() ;
		
		System.out.println( c.isEmpty() + " , " + c.size() );
		
		c.add( "唐三藏" ); // 添加元素
		c.add( "孙悟空" ); 
		c.add( "白龙马" ); 
		c.add( "猪八戒" );
		c.add( "沙悟净" );
		
		System.out.println( c.toString() );

		System.out.println( c.isEmpty() + " , " + c.size() );
		
		c.remove( "孙悟空" );
		
		System.out.println( c.toString() );
		
		// 获得可以迭代指定集合的迭代器
		Iterator<String> itor = c.iterator();
		// 使用迭代器迭代集合
		while( itor.hasNext() ) {
			String name = itor.next();
			System.out.println( name );
		}
		
		System.out.println( itor.hasNext() );
		
		System.out.println( c.contains( "孙悟空" ) );
		System.out.println( c.contains( "猪悟能" ) );
		System.out.println( c.contains( "猪八戒" ) );
		
		c.clear(); // 清空集合
		
		System.out.println( c.isEmpty() + " , " + c.size() );
	}

}
